<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Cms_management extends MX_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model(array('admin_commoncall','admin_settings_model','admin_cms_model'));
		
		$this->admin_commoncall->check_admin_login();
		$this->template->set_theme('admin');
		$this->template->set_layout('default');	
		
		
	}
	
	function index()
	{
	
		$data['cms'] = $this->admin_cms_model->get_cms();
		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		->build('cms/cms_management',$data);
		
	}
	
	function get_cms() {
		
		$data['cms'] = $this->admin_cms_model->get_cms();
		
		if ($this->input->post('cms')) {
			$this->form_validation->set_rules('cms', 'cms', 'required|xss_clean');
			$title = $this->input->post('cms');
			if ($this->form_validation->run()) {
				$sql="select * from cms where title='$title'";
				$query=$this->db->query($sql);
				$data['cms']['info']=$query->row();
				
				$data['cms_id']= $data['cms']['info']->id;
				
				$this->template->title('Welcome', 'Ypage.com')
				->set_partial('nav_bar','layouts/nav_bar')
				->set_partial('side_menu','layouts/side_menu')
					
				->build('cms/cms_management', $data);
			}
				
		}
		else{
			$this->session->set_flashdata('message', 'Please Choose a cms');
			redirect('secure/cms/cms_management');
		}
	
	}
	function update_cms() {
	
		if ($this->input->post('cms_update')) {
			$this->form_validation->set_rules('cmscontent', 'Description', 'required|xss_clean');
			$this->form_validation->set_rules('cms_id', 'cms_id', 'required|xss_clean');
			$this->form_validation->set_rules('position','Select Position','required|xss_clean'); 
			if ($this->form_validation->run()) {
				if($this->input->post('url'))
					$url=$this->input->post('url');
				else
					$url=$this->input->post('title');
				$data = array(
						'title'=>trim($this->input->post('title')),
						'url'=>$url,
						'meta_title'=>trim($this->input->post('meta_title')),
						'meta_keyword'=>trim($this->input->post('meta_keyword')),
						'meta_description'=>trim($this->input->post('meta_description')),
						'description' => trim($this->input->post('cmscontent')),
						'position' => trim($this->input->post('position'))
						
				);
				$this->db->where('id', trim($this->input->post('cms_id')));
				$this->db->update('cms', $data);
				$data['cms']['management'] = $this->admin_cms_model->get_cms();
				$this->session->set_flashdata('message', 'CMS Updated Sucessfully..........');
				redirect('secure/cms/cms_management');
	
			}else{
	
				$this->template->build('cms/cms_management', $data);
			}
				
		}
	
	}
	
	
}